<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>playwright | East'blog</title><meta name="author" content="侯玉东"><meta name="copyright" content="侯玉东"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="playwright">
<meta property="og:type" content="article">
<meta property="og:title" content="playwright">
<meta property="og:url" content="https://houyudong1989.gitee.io/blog/2024/04/26/python/13%E3%80%81playwright/index.html">
<meta property="og:site_name" content="East&#39;blog">
<meta property="og:description" content="playwright">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://houyudong1989.gitee.io/blog/img/library1.jpeg">
<meta property="article:published_time" content="2024-04-26T07:30:36.069Z">
<meta property="article:modified_time" content="2024-05-05T01:47:43.249Z">
<meta property="article:author" content="侯玉东">
<meta property="article:tag" content="playwright">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://houyudong1989.gitee.io/blog/img/library1.jpeg"><link rel="shortcut icon" href="/blog/img/head.jpg"><link rel="canonical" href="https://houyudong1989.gitee.io/blog/2024/04/26/python/13%E3%80%81playwright/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'playwright',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-05-05 09:47:43'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (true) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/head.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">70</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友联</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('/blog/img/library2.jpeg')"><nav id="nav"><span id="blog-info"><a href="/blog/" title="East'blog"><img class="site-icon" src="/blog/img/head.jpg"/><span class="site-name">East'blog</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友联</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">playwright</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-04-26T07:30:36.069Z" title="发表于 2024-04-26 15:30:36">2024-04-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-05-05T01:47:43.249Z" title="更新于 2024-05-05 09:47:43">2024-05-05</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/python-%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95-%E7%88%AC%E8%99%AB-playwright/">python 自动化测试 爬虫 playwright</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="playwright"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="playwright"><a href="#playwright" class="headerlink" title="playwright"></a>playwright</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Playwright 是一个用于自动化浏览器操作的开源工具，由 Microsoft 开发和维护。它支持多种浏览器（包括 Chromium、Firefox 和 WebKit）和多种编程语言（如 Python、JavaScript 和 C#），可以用于测试、爬虫、自动化任务等场景。 </span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>与 Selenium 和 pyppeteer 相比，Playwright 具有以下几个区别和优势：</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1. 多浏览器支持：支持所有主流浏览器。这使得开发人员可以根据需求选择最适合的浏览器进行自动化操作。（Playwright不支持旧版Microsoft Edge或IE11）</span><br><span class="line">2. 更快的执行速度：Playwright 通过使用浏览器的底层调试协议来进行操作，相比于 Selenium 和 pyppeteer，它具有更快的执行速度和更低的资源消耗。</span><br><span class="line">3. 可靠性和稳定性：Playwright 提供了更可靠和稳定的浏览器自动化操作，通过使用浏览器的原生 API 来模拟用户行为，避免了一些传统自动化工具的一些限制和不稳定性。</span><br><span class="line">4. 支持跨浏览器和跨平台：Playwright 可以在不同浏览器和不同操作系统上运行，这使得开发人员可以更方便地进行跨浏览器和跨平台的测试和自动化操作。</span><br><span class="line">5. 支持屏幕录制功能：根据屏幕录制指定生成相关操作代码。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>爬虫中使用 Playwright 的好处</strong>:</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1. 动态网页爬取：Playwright 可以模拟用户在浏览器中的操作，包括渲染 JavaScript、点击按钮、填写表单等，从而可以爬取包含动态内容的网页。</span><br><span class="line">2. 多浏览器支持：Playwright 支持多种浏览器，可以根据需求选择最适合的浏览器进行爬取，以确保爬取结果的准确性和一致性。</span><br><span class="line">3. 更高的稳定性和可靠性：Playwright 使用浏览器的原生 API 进行操作，避免了一些传统爬虫工具的一些限制和不稳定性，提供了更可靠和稳定的爬取能力。</span><br><span class="line"></span><br><span class="line">总之，Playwright 是一个功能强大、跨浏览器、跨平台的浏览器自动化工具，相比于 Selenium 和 pyppeteer，它具有更快的执行速度、更高的稳定性和更广泛的浏览器支持，适用于多种自动化操作和爬虫场景。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>python版本的Playwright官网文档：</strong><a target="_blank" rel="noopener" href="https://playwright.dev/python/docs/intro">https://playwright.dev/python/docs/intro</a></p>
</blockquote>
<h2 id="环境安装"><a href="#环境安装" class="headerlink" title="环境安装"></a>环境安装</h2><blockquote>
<p><strong>系统要求：</strong></p>
</blockquote>
<ul>
<li>Python 3.8 或更高版本。</li>
<li>Windows 10+、Windows Server 2016+ 或适用于 Linux 的 Windows 子系统 （WSL）。</li>
<li>MacOS 12 Monterey 或 MacOS 13 Ventura。</li>
<li>Debian 11、Debian 12、Ubuntu 20.04 或 Ubuntu 22.04。</li>
</ul>
<blockquote>
<p><strong>安装playwright的python版本</strong></p>
</blockquote>
<ul>
<li>pip install playwright</li>
</ul>
<blockquote>
<p><strong>安装Playwright所需的所有工具插件和所支持的浏览器</strong></p>
</blockquote>
<ul>
<li>playwright install</li>
<li>该步骤耗时较长</li>
</ul>
<h2 id="屏幕录制"><a href="#屏幕录制" class="headerlink" title="屏幕录制"></a>屏幕录制</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">playwright提供一种高级功能，就是可以将我们的操作录制下来，生成代码，方便我们后续使用。有一定的局限性，了解即可。</span><br></pre></td></tr></table></figure>

<h3 id="录制流程"><a href="#录制流程" class="headerlink" title="录制流程"></a>录制流程</h3><blockquote>
<p>1.在pycharm中创建工作文件，起名为playwright.py。</p>
</blockquote>
<blockquote>
<p>2.进入终端，进入playwright.py所在文件目录。</p>
</blockquote>
<blockquote>
<p>3.执行命令： playwright codegen -o playwright.py 。</p>
</blockquote>
<blockquote>
<p>4.此时会自动弹出浏览器页面，点击录制按钮，开始录制。</p>
</blockquote>
<p><img src="1714118613966.png" alt="1714118613966"></p>
<blockquote>
<p>5.在浏览器中输入网址，完成需要录制的步骤过程，点击停止录制按钮，完成录制。</p>
</blockquote>
<blockquote>
<p>6.完成录制后，自动生成的代码会写入playwright.py文件中 。</p>
</blockquote>
<h3 id="录制命令"><a href="#录制命令" class="headerlink" title="录制命令"></a>录制命令</h3><blockquote>
<p>将屏幕录制生成的代码保存到playwright文件中</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">playwright codegen -o playwright.py   </span><br></pre></td></tr></table></figure>

<blockquote>
<p>模拟手机设备进行网络请求（只支持手机模拟器，无需单独安装）</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">playwright codegen --device=&quot;iPhone 13&quot; -o playwright.py </span><br></pre></td></tr></table></figure>

<p><strong>支持设备列表</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&quot;Blackberry PlayBook&quot;,  &quot;Blackberry PlayBook landscape&quot;,  &quot;BlackBerry Z30&quot;,  &quot;BlackBerry Z30 landscape&quot;,  &quot;Galaxy Note 3&quot;,  &quot;Galaxy Note 3 landscape&quot;,  &quot;Galaxy Note II&quot;,  &quot;Galaxy Note II landscape&quot;,  &quot;Galaxy S III&quot;,  &quot;Galaxy S III landscape&quot;,  &quot;Galaxy S5&quot;,  &quot;Galaxy S5 landscape&quot;,  &quot;Galaxy S8&quot;,  &quot;Galaxy S8 landscape&quot;,  &quot;Galaxy S9+&quot;,  &quot;Galaxy S9+ landscape&quot;,  &quot;Galaxy Tab S4&quot;,  &quot;Galaxy Tab S4 landscape&quot;,  &quot;iPad (gen 5)&quot;,  &quot;iPad (gen 5) landscape&quot;,  &quot;iPad (gen 6)&quot;,  &quot;iPad (gen 6) landscape&quot;,  &quot;iPad (gen 7)&quot;,  &quot;iPad (gen 7) landscape&quot;,  &quot;iPad Mini&quot;,  &quot;iPad Mini landscape&quot;,  &quot;iPad Pro 11&quot;,  &quot;iPad Pro 11 landscape&quot;,  &quot;iPhone 6&quot;,  &quot;iPhone 6 landscape&quot;,  &quot;iPhone 6 Plus&quot;,  &quot;iPhone 6 Plus landscape&quot;,  &quot;iPhone 7&quot;,  &quot;iPhone 7 landscape&quot;,  &quot;iPhone 7 Plus&quot;,  &quot;iPhone 7 Plus landscape&quot;,  &quot;iPhone 8&quot;,  &quot;iPhone 8 landscape&quot;,  &quot;iPhone 8 Plus&quot;,  &quot;iPhone 8 Plus landscape&quot;,  &quot;iPhone SE&quot;,  &quot;iPhone SE landscape&quot;,  &quot;iPhone X&quot;,  &quot;iPhone X landscape&quot;,  &quot;iPhone XR&quot;,  &quot;iPhone XR landscape&quot;,  &quot;iPhone 11&quot;,  &quot;iPhone 11 landscape&quot;,  &quot;iPhone 11 Pro&quot;,  &quot;iPhone 11 Pro landscape&quot;,  &quot;iPhone 11 Pro Max&quot;,  &quot;iPhone 11 Pro Max landscape&quot;,  &quot;iPhone 12&quot;,  &quot;iPhone 12 landscape&quot;,  &quot;iPhone 12 Pro&quot;,  &quot;iPhone 12 Pro landscape&quot;,  &quot;iPhone 12 Pro Max&quot;,  &quot;iPhone 12 Pro Max landscape&quot;,  &quot;iPhone 12 Mini&quot;,  &quot;iPhone 12 Mini landscape&quot;,  &quot;iPhone 13&quot;,  &quot;iPhone 13 landscape&quot;,  &quot;iPhone 13 Pro&quot;,  &quot;iPhone 13 Pro landscape&quot;,  &quot;iPhone 13 Pro Max&quot;,  &quot;iPhone 13 Pro Max landscape&quot;,  &quot;iPhone 13 Mini&quot;,  &quot;iPhone 13 Mini landscape&quot;,  &quot;iPhone 14&quot;,  &quot;iPhone 14 landscape&quot;,  &quot;iPhone 14 Plus&quot;,  &quot;iPhone 14 Plus landscape&quot;,  &quot;iPhone 14 Pro&quot;,  &quot;iPhone 14 Pro landscape&quot;,  &quot;iPhone 14 Pro Max&quot;,  &quot;iPhone 14 Pro Max landscape&quot;,  &quot;Kindle Fire HDX&quot;,  &quot;Kindle Fire HDX landscape&quot;,  &quot;LG Optimus L70&quot;,  &quot;LG Optimus L70 landscape&quot;,  &quot;Microsoft Lumia 550&quot;,  &quot;Microsoft Lumia 550 landscape&quot;,  &quot;Microsoft Lumia 950&quot;,  &quot;Microsoft Lumia 950 landscape&quot;,  &quot;Nexus 10&quot;,  &quot;Nexus 10 landscape&quot;,  &quot;Nexus 4&quot;,  &quot;Nexus 4 landscape&quot;,  &quot;Nexus 5&quot;,  &quot;Nexus 5 landscape&quot;,  &quot;Nexus 5X&quot;,  &quot;Nexus 5X landscape&quot;,  &quot;Nexus 6&quot;,  &quot;Nexus 6 landscape&quot;,  &quot;Nexus 6P&quot;,  &quot;Nexus 6P landscape&quot;,  &quot;Nexus 7&quot;,  &quot;Nexus 7 landscape&quot;,  &quot;Nokia Lumia 520&quot;,  &quot;Nokia Lumia 520 landscape&quot;,  &quot;Nokia N9&quot;,  &quot;Nokia N9 landscape&quot;,  &quot;Pixel 2&quot;,  &quot;Pixel 2 landscape&quot;,  &quot;Pixel 2 XL&quot;,  &quot;Pixel 2 XL landscape&quot;,  &quot;Pixel 3&quot;,  &quot;Pixel 3 landscape&quot;,  &quot;Pixel 4&quot;,  &quot;Pixel 4 landscape&quot;,  &quot;Pixel 4a (5G)&quot;,  &quot;Pixel 4a (5G) landscape&quot;,  &quot;Pixel 5&quot;,  &quot;Pixel 5 landscape&quot;,  &quot;Pixel 7&quot;,  &quot;Pixel 7 landscape&quot;,  &quot;Moto G4&quot;,  &quot;Moto G4 landscape&quot;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>访问指定网址，并且设置浏览器窗口大小</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">playwright codegen --viewport-size=800,600  www.baidu.com -o 指定文件.py   </span><br></pre></td></tr></table></figure>

<h2 id="保留cookie信息"><a href="#保留cookie信息" class="headerlink" title="保留cookie信息"></a>保留cookie信息</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">有些网站需要登录后才能访问，可以通过--save-storage=auth.json和--load-storage=auth.json来保存和加载cookie信息，实现自动登录。</span><br></pre></td></tr></table></figure>

<blockquote>
<p>执行命令–save-storage&#x3D;auth.json 在屏幕录制时，进行登录操作，保存登录后的cookie信息到auth.json文件中。</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">playwright codegen --save-storage=auth.json http://download.java1234.com/ </span><br></pre></td></tr></table></figure>

<blockquote>
<p>执行命令–load-storage&#x3D;auth.json 加载auth.json文件中的cookie信息，实现自动登录，进入登录成功的页面。</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">playwright codegen --load-storage=auth.json http://download.java1234.com/ -o playwright.py</span><br></pre></td></tr></table></figure>

<h2 id="第一个playwright程序"><a href="#第一个playwright程序" class="headerlink" title="第一个playwright程序"></a>第一个playwright程序</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">from</span> playwright.sync_api <span class="keyword">import</span>  sync_playwright</span><br><span class="line"><span class="keyword">def</span> <span class="title function_">run</span>() :</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        browser = p.chromium.launch(headless=<span class="literal">True</span>,slow_mo=<span class="number">100</span>)</span><br><span class="line">	<span class="comment"># p.chromium.launch p 是一个 Playwright 对象，chromium 表示要使用 Chromium 浏览器，launch表示启动一个Chromium浏览器实例</span></span><br><span class="line">	<span class="comment"># 当 headless=True 时，浏览器会在后台运行，没有可见的图形用户界面。这对于自动化测试和网页爬取等任务非常有用，因为它可以减少资源消耗，并且不会在屏幕上显#示浏览器窗口。</span></span><br><span class="line">	<span class="comment"># 当 headless=False 时，浏览器将以常规的有头模式启动，显示图形用户界面，你可以在屏幕上看到浏览器窗口。</span></span><br><span class="line">	<span class="comment"># slow_mo 是 Playwright 中用于设置慢速模式,设置每个操作延时等待100毫秒。</span></span><br><span class="line"></span><br><span class="line">        page = browser.new_page()</span><br><span class="line">	<span class="comment"># 浏览器实例创建一个新的页面</span></span><br><span class="line">        page.goto(<span class="string">&quot;http://www.baidu.com/&quot;</span>)</span><br><span class="line">	<span class="comment"># 浏览器页面进入百度首页</span></span><br><span class="line"> 	page.wait_for_timeout(<span class="number">1000</span>)</span><br><span class="line">	<span class="comment">#设置等待时长，单位毫秒</span></span><br><span class="line">    	title = page.title()</span><br><span class="line">	<span class="comment"># 获取百度首页标题</span></span><br><span class="line">        page_content = page.content()</span><br><span class="line">	<span class="comment"># 获取百度首页所有内容</span></span><br><span class="line">        <span class="built_in">print</span>(title,page_content)</span><br><span class="line">	<span class="comment"># 打印输出</span></span><br><span class="line">        <span class="comment"># ---------------------</span></span><br><span class="line">        page.close()</span><br><span class="line">        browser.close()</span><br><span class="line">	<span class="comment"># 关闭页面，关闭浏览器</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    run( )</span><br></pre></td></tr></table></figure>

<h2 id="定位元素"><a href="#定位元素" class="headerlink" title="定位元素"></a>定位元素</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">* playwright是模拟用户点击操作，所以需要定位元素，才能进行点击操作。通过css样式和xpath两种方式来定位元素。获取到元素后可以对元素进行各种操作。如：点击，输入文本，滑动，获取文本等。</span><br><span class="line"></span><br><span class="line">* 在页面上通过调用page.locator()函数获取到元素，然后调用元素上的方法进行操作。page代表当前页面对象</span><br></pre></td></tr></table></figure>

<h3 id="CSS定位"><a href="#CSS定位" class="headerlink" title="CSS定位"></a>CSS定位</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">* 通过css样式来定位元素，css样式是页面上元素的唯一标识，通过css样式可以唯一定位页面上的元素。可以使用标签、id、层级和class选择器多种方式来定位元素。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>标签</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">page.locator(&#x27;input#kw&#x27;)</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>id</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">page.locator(&#x27;#kw&#x27;)</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>class选择器</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">page.locator(&#x27;.s_ipt&#x27;)</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>层级</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">page.locator(&#x27;form &gt; span &gt; input&#x27;)</span><br></pre></td></tr></table></figure>

<h4 id="css定位示例"><a href="#css定位示例" class="headerlink" title="css定位示例"></a>css定位示例</h4><p><img src="1714183108114.png" alt="1714183108114"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># css定位</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">css_location</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        browser = p.chromium.launch(headless=<span class="literal">False</span>, slow_mo=<span class="number">1000</span>)</span><br><span class="line">        page = browser.new_page()</span><br><span class="line">        page.goto(<span class="string">&quot;http://www.baidu.com/&quot;</span>)</span><br><span class="line">        locator = page.locator(<span class="string">&quot;.s_ipt&quot;</span>) </span><br><span class="line">        <span class="comment"># &lt;class &#x27;playwright.sync_api._generated.Locator&#x27;&gt;   类选择器 数量为[0,N]</span></span><br><span class="line">        locator = page.locator(<span class="string">&quot;#kw&quot;</span>) </span><br><span class="line">        <span class="comment"># &lt;class &#x27;playwright.sync_api._generated.Locator&#x27;&gt;   id选择器 数量为[0,1]</span></span><br><span class="line">        locator = page.locator(<span class="string">&quot;input&quot;</span>) </span><br><span class="line">        <span class="comment"># &lt;class &#x27;playwright.sync_api._generated.Locator&#x27;&gt;  标签选择器 数量为[0,N]</span></span><br><span class="line">        locator = page.locator(<span class="string">&quot;input#kw&quot;</span>)</span><br><span class="line">        <span class="comment"># &lt;class &#x27;playwright.sync_api._generated.Locator&#x27;&gt;  标签+id复合选择器 数量为[0,1]</span></span><br><span class="line">        locator = page.locator(<span class="string">&quot;input.s_ipt&quot;</span>) </span><br><span class="line">        <span class="comment"># &lt;class &#x27;playwright.sync_api._generated.Locator&#x27;&gt;  标签+类复合选择器 数量为[0,N]</span></span><br><span class="line">        locator = page.locator(<span class="string">&quot;body&gt;div&gt;div&gt;div&gt;div&quot;</span>)</span><br><span class="line">        <span class="comment"># &lt;class &#x27;playwright.sync_api._generated.Locator&#x27;&gt;  层级选择器 数量为[0,N]</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="built_in">type</span>(locator))</span><br><span class="line">        <span class="built_in">print</span>(locator.count())</span><br><span class="line">        <span class="comment"># ---------------------</span></span><br><span class="line">        page.close()</span><br><span class="line">        browser.close()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="xpath定位"><a href="#xpath定位" class="headerlink" title="xpath定位"></a>xpath定位</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">page.locator(&#x27;//*[@id=&quot;nav-searchform&quot;]/div[1]/input&#x27;)</span><br></pre></td></tr></table></figure>

<h4 id="xpath定位示例"><a href="#xpath定位示例" class="headerlink" title="xpath定位示例"></a>xpath定位示例</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># xpath定位  </span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">xpath_location</span>():</span><br><span class="line">  <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">      browser = p.chromium.launch(headless=<span class="literal">True</span>)</span><br><span class="line">      page = browser.new_page()</span><br><span class="line">      page.goto(<span class="string">&quot;http://www.baidu.com&quot;</span>)</span><br><span class="line"><span class="comment"># 通过xpath定位元素</span></span><br><span class="line">      locator = page.locator(<span class="string">&quot;//input[@class=&#x27;s_ipt&#x27;]&quot;</span>)</span><br><span class="line">      <span class="built_in">print</span>(<span class="built_in">type</span>(locator))</span><br><span class="line">      <span class="built_in">print</span>(locator.count())</span><br><span class="line">      <span class="comment"># ---------------------</span></span><br><span class="line">      page.close()</span><br><span class="line">      browser.close()</span><br></pre></td></tr></table></figure>

<h3 id="根据文本定位"><a href="#根据文本定位" class="headerlink" title="根据文本定位"></a>根据文本定位</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">通过调用get_by_text函数，可以定位页面上文本为“指定内容”的元素。</span><br><span class="line">   page.get_by_text(&#x27;发货地&#x27;)</span><br></pre></td></tr></table></figure>

<h4 id="文本定位示例"><a href="#文本定位示例" class="headerlink" title="文本定位示例"></a>文本定位示例</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 通过文本查询元素</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">text_location</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        browser = p.chromium.launch(headless=<span class="literal">False</span>)</span><br><span class="line">        page = browser.new_page()</span><br><span class="line">        page.goto(<span class="string">&quot;http://www.baidu.com&quot;</span>)</span><br><span class="line">        locator = page.get_by_text(<span class="string">&quot;百度一下&quot;</span>) <span class="comment">#只能查找文本内容，不能匹配属性中的文本</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="built_in">type</span>(locator))</span><br><span class="line">        <span class="built_in">print</span>(locator.count())</span><br><span class="line">        <span class="comment"># ---------------------</span></span><br><span class="line">        page.close()</span><br><span class="line">        browser.close()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="iframe定位"><a href="#iframe定位" class="headerlink" title="iframe定位"></a>iframe定位</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">iframe 简单来说就是一个 html 嵌套了另外一个 html。在页面元素上最简单的识别方法，就是看你需要定位的元素外层有没有iframe的标签名称。 </span><br><span class="line">测试网址:http://download.java1234.com/</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.frames</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">获取page页面有多少个frames。</span><br><span class="line">    [&lt;Frame name= url=&#x27;http://download.java1234.com/&#x27;&gt;, &lt;Frame name=layui-layer-iframe1 url=&#x27;http://download.java1234.com/login.html&#x27;&gt;]</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.main_frame</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">获取当前页面的主frame。</span><br><span class="line">    &lt;Frame name= url=&#x27;http://download.java1234.com/&#x27;&gt;</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.main_frame.child_Frames</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">获取当前页面的主frame所有子frame。</span><br><span class="line">[&lt;Frame name=layui-layer-iframe1 url=&#x27;http://download.java1234.com/login.html&#x27;&gt;]</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.frame(name,url)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">iframe有两个重要的属性，name和url。如果属性不存在，输出为空。</span><br><span class="line">可以通过iframe.name或者iframe.url来定位iframe。</span><br><span class="line">    frame = page.frame(name=&quot;layui-layer-iframe1&quot;)</span><br><span class="line">    frame = page.frame(url=&quot;http://download.java1234.com/login.html&quot;)</span><br></pre></td></tr></table></figure>

<h4 id="frame示例"><a href="#frame示例" class="headerlink" title="frame示例"></a>frame示例</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">login</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        browser =p.chromium.launch(headless=<span class="literal">False</span>)</span><br><span class="line">        page = browser.new_page()</span><br><span class="line">        page.goto(<span class="string">&quot;http://download.java1234.com/&quot;</span>)</span><br><span class="line">        login_btn = page.get_by_text(<span class="string">&quot;登录&quot;</span>)</span><br><span class="line">        login_btn.click()</span><br><span class="line">        page.wait_for_timeout(<span class="number">3400</span>)</span><br><span class="line">        <span class="built_in">print</span>(page.frames )</span><br><span class="line">        <span class="built_in">print</span>(page.main_frame )</span><br><span class="line">        <span class="built_in">print</span>(page.main_frame.child_frames )</span><br><span class="line"></span><br><span class="line">        frame = page.frame(name=<span class="string">&quot;layui-layer-iframe1&quot;</span>)</span><br><span class="line">        <span class="comment"># frame = page.frame(url=&quot;http://download.java1234.com/login.html&quot;)</span></span><br><span class="line">  </span><br><span class="line">        username = frame.locator(<span class="string">&quot;#userName&quot;</span>) </span><br><span class="line">        username .fill(<span class="string">&quot;east&quot;</span>)</span><br><span class="line">        pwd = frame.locator(<span class="string">&quot;#password&quot;</span>) </span><br><span class="line">        pwd.fill(<span class="string">&quot;123456&quot;</span>)</span><br><span class="line">        frame.locator(<span class="string">&quot;.layui-btn&quot;</span>).click()</span><br><span class="line">        page.wait_for_timeout(<span class="number">2000</span>)</span><br><span class="line"></span><br><span class="line">        page.close()</span><br><span class="line">        browser.close()</span><br></pre></td></tr></table></figure>

<h3 id="其他操作"><a href="#其他操作" class="headerlink" title="其他操作"></a>其他操作</h3><blockquote>
<p><strong>locator.all()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">该方法用于获取所有匹配特定定位器条件的元素。它返回一个元素列表，你可以对列表中的每个元素执行操作。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>locator.count()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">该方法返回匹配特定定位器条件的元素数量。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>locator.nth(index)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">该方法用于获取匹配特定定位器条件的元素列表中的第 index 个元素。索引从零开始。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>inner_text()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取标签中的文本内容。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>get_attribute(attrName)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取标签指定属性的值。</span><br></pre></td></tr></table></figure>

<h4 id="其他操作示例"><a href="#其他操作示例" class="headerlink" title="其他操作示例"></a>其他操作示例</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 其他函数示例</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">other_demo</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        browser = p.chromium.launch(headless=<span class="literal">True</span>)</span><br><span class="line">        page = browser.new_page()</span><br><span class="line">        page.goto(<span class="string">&quot;http://www.baidu.com&quot;</span>)</span><br><span class="line">        locator = page.locator(<span class="string">&quot;input&quot;</span>)  <span class="comment">#查找的元素无论是多少个，他都返回的是Locator对象</span></span><br><span class="line">        count = locator.count() <span class="comment"># 获取元素个数</span></span><br><span class="line">        <span class="built_in">print</span>(count)</span><br><span class="line">        <span class="built_in">list</span> = locator.<span class="built_in">all</span>() <span class="comment">#获取locator对象中获取到的所有元素，以列表形式返回</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="built_in">list</span>)</span><br><span class="line">        tag = locator.nth(<span class="number">12</span>) <span class="comment">#获取locator对象中指定下标元素，从0开始</span></span><br><span class="line">        <span class="built_in">print</span>(tag)</span><br><span class="line"></span><br><span class="line">        attr_value = tag.get_attribute(<span class="string">&quot;value&quot;</span>) <span class="comment">#获取tag标签value属性的值</span></span><br><span class="line">        attr_name = tag.get_attribute(<span class="string">&quot;name&quot;</span>) <span class="comment">#获取tag标签name属性的值</span></span><br><span class="line">        <span class="built_in">print</span>(attr_value,attr_name)</span><br><span class="line"> </span><br><span class="line">        locator = page.locator(<span class="string">&quot;html&gt;head&gt;title&quot;</span>)  <span class="comment"># 获取title标签 </span></span><br><span class="line">        text = locator.inner_text() <span class="comment"># 获取title标签内的文本</span></span><br><span class="line">        <span class="built_in">print</span>(text)</span><br><span class="line">        <span class="comment"># ---------------------</span></span><br><span class="line">        page.close()</span><br><span class="line">        browser.close()</span><br></pre></td></tr></table></figure>

<h2 id="page-常用函数"><a href="#page-常用函数" class="headerlink" title="page 常用函数"></a>page 常用函数</h2><blockquote>
<p><strong>goto(url, options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">导航到指定的 URL。可以通过传递选项参数来配置导航行为，例如设置超时时间等。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>bring_to_front()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">停留在当前页面。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>reload(options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">重新加载当前页面。同样可以通过选项参数配置重新加载行为。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>close(options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">关闭当前页面。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>locator(options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">通过指定规则定位元素。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>title()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取当前页面的标题。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>content()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取当前页面的页面内容数据。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>url()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取当前页面的 URL。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>click(selector, options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">点击匹配给定选择器的元素。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>fill(selector, value, options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在匹配给定选择器的表单元素中填入指定的值。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>go_forward()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">进入下一页（之前访问过的）。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>go_back()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">返回到上一页。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>screenshot(options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">对当前页面进行截图。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>evaluate(pageFunction, …args)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在页面上下文中执行 JavaScript 函数，并返回结果。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.mouse.click(x, y, options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">模拟在指定坐标位置点击鼠标左键。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.mouse.dblclick(x, y, options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">模拟在指定坐标位置双击鼠标左键。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.mouse.move（x,y）</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">鼠标操作 移动到指定坐标</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.mouse.down()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">鼠标操作 按下鼠标</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.mouse.up()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">鼠标操作 释放鼠标</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.mouse.wheel(deltaX, deltaY)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">鼠标操作 模拟滚动鼠标滚轮。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.keyboard.type(text, options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">键盘操作 模拟键盘输入指定的文本。可以通过 options 参数设置输入的延迟时间控制每个字符输入后等待时长。 page.keyboard.type(text,delay=1000);</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.keyboard.press(key, options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">键盘操作 模拟按下指定的按键。可以通过 options 参数来设置按键的修饰键，比如 Ctrl、Alt、Shift 等。page.keyboard.press(&#x27;Enter&#x27;);</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.keyboard.down(key)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">键盘操作 模拟按下指定的按键，但不释放。page.keyboard.down(&#x27;Shift&#x27;);</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.keyboard.up(key)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">键盘操作 释放之前按下的指定按键。page.keyboard.up(&#x27;Shift&#x27;);</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>page.keyboard.insertText(text)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">键盘操作 在焦点元素中插入指定的文本。与 type 方法类似，但不考虑焦点元素。page.keyboard.insertText(&#x27;Hello, world!&#x27;);</span><br></pre></td></tr></table></figure>

<h2 id="tag常用函数"><a href="#tag常用函数" class="headerlink" title="tag常用函数"></a>tag常用函数</h2><blockquote>
<p><strong>textContent()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取元素内部的文本内容。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>getAttribute(name)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取元素的指定属性值。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>click(options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">模拟点击元素。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>hover(options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">将鼠标悬停在元素上。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>scrollIntoViewIfNeeded(options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">将元素滚动到可见区域。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>isIntersectingViewport()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">检查元素是否与视口相交（即是否在可见区域）。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>isVisible()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">检查元素是否可见。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>type(text, options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在元素上输入指定的文本。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>selectOption(values, options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">选择下拉菜单中的指定选项。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>focus(options)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">将焦点设置到元素上。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>isEnabled()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">检查元素是否可用（未禁用）。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>isChecked()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">检查复选框或单选框是否被选中。</span><br></pre></td></tr></table></figure>

<h2 id="context"><a href="#context" class="headerlink" title="context"></a>context</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">浏览器的上下文管理对象Context可以用于管理Context打开/创建的多个page页面。并且可以创建多个Context对象，那么不同的Context对象打开/创建的page之间是相互隔离的（每个Context上下文都有自己的Cookie、浏览器存储和浏览历史记录）。</span><br></pre></td></tr></table></figure>

<h3 id="context作用"><a href="#context作用" class="headerlink" title="context作用"></a>context作用</h3><blockquote>
<p><strong>多个用户会话模拟</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">你可以在同一浏览器中创建多个上下文，每个上下文代表一个用户会话，从而实现并行测试或模拟多用户场景。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>Cookie 管理</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">每个上下文都有自己的 Cookie 存储，你可以在不同的上下文中管理和使用 Cookie，例如模拟用户登录状态或进行不同用户之间的数据交互。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>隔离页面集合</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">每个上下文都有自己的页面集合，页面之间相互隔离，可以独立进行操作和测试，提高测试效率和可靠性。</span><br></pre></td></tr></table></figure>

<h3 id="context常用函数"><a href="#context常用函数" class="headerlink" title="context常用函数"></a>context常用函数</h3><blockquote>
<p><strong>newContext([options])</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">创建一个新的上下文。可以通过选项参数配置上下文的行为，例如设置用户代理、浏览器视口大小等。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>browserContexts()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取当前浏览器实例中的所有上下文列表。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>newPage([options])</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在当前上下文中创建一个新页面。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>close()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">关闭当前上下文。关闭上下文将关闭其所有页面并清除相关资源。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>addCookies(cookies)</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">向当前上下文添加 Cookie。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>clearCookies()</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">清除当前上下文中的所有 Cookie。</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>storageState([options])</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取当前上下文的存储状态，包括 Cookie、LocalStorage、SessionStorage 等。</span><br></pre></td></tr></table></figure>

<h3 id="context示例"><a href="#context示例" class="headerlink" title="context示例"></a>context示例</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">context_demo</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        webbrowser = p.chromium.launch(headless=<span class="literal">False</span>)</span><br><span class="line">        context = webbrowser.new_context()</span><br><span class="line">        page = context.new_page()</span><br><span class="line">        page.goto(<span class="string">&quot;http://www.muluyu.com/&quot;</span>)</span><br><span class="line">        locator = page.locator(<span class="string">&quot;//div[@class=&#x27;recommds&#x27;]/li&quot;</span>)</span><br><span class="line">        <span class="keyword">for</span> index <span class="keyword">in</span> <span class="built_in">range</span>(locator.count()):</span><br><span class="line">            locator.nth(index).click()</span><br><span class="line">            page.wait_for_timeout(<span class="number">3000</span>)  <span class="comment"># 停顿一下，避免时间太快下面获取不到title</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> page <span class="keyword">in</span> context.pages:</span><br><span class="line">            <span class="built_in">print</span>(page.title())</span><br><span class="line">            <span class="keyword">if</span> <span class="string">&quot;人人影视分享下载站&quot;</span> == page.title():</span><br><span class="line">                page.bring_to_front()  <span class="comment">#进入指定页面</span></span><br><span class="line">            <span class="comment"># ---------------------</span></span><br><span class="line">        <span class="built_in">input</span>()</span><br><span class="line">        page.close()</span><br><span class="line">        webbrowser.close()</span><br></pre></td></tr></table></figure>

<h2 id="规避检测"><a href="#规避检测" class="headerlink" title="规避检测"></a>规避检测</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">如果网站有对Playwright采取监测机制的话，比如正常情况下我们用浏览器访问淘宝等网站的 window.navigator.webdriver的值为 undefined或者为false。而使用Playwright访问则该值为true。可以通过js注入的方式解决该问题。</span><br></pre></td></tr></table></figure>

<h3 id="查看taobao是否检测Playwright"><a href="#查看taobao是否检测Playwright" class="headerlink" title="查看taobao是否检测Playwright"></a>查看taobao是否检测Playwright</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">taobao</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        bro = p.chromium.launch(headless=<span class="literal">False</span>)</span><br><span class="line">        page = bro.new_page()</span><br><span class="line">        page.goto(<span class="string">&#x27;https://www.taobao.com/&#x27;</span>)</span><br><span class="line">        ret = page.evaluate(<span class="string">&#x27;window.navigator.webdriver&#x27;</span>)</span><br><span class="line">        <span class="built_in">print</span>(ret)</span><br><span class="line">        page.wait_for_timeout(<span class="number">30000</span>)</span><br><span class="line">        page.close()</span><br><span class="line">        bro.close()</span><br></pre></td></tr></table></figure>

<h3 id="js注入，绕过淘宝检测"><a href="#js注入，绕过淘宝检测" class="headerlink" title="js注入，绕过淘宝检测"></a>js注入，绕过淘宝检测</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">taobao2</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        bro = p.chromium.launch(headless=<span class="literal">False</span>)</span><br><span class="line">        context = bro.new_context()</span><br><span class="line">        <span class="comment"># 加载该js文件目的是给Playwright的浏览器模拟真实的浏览器环境</span></span><br><span class="line">        context.add_init_script(path=<span class="string">&#x27;./stealth.min.js&#x27;</span>)</span><br><span class="line">        page = context.new_page()</span><br><span class="line">        page.goto(<span class="string">&#x27;https://www.taobao.com/&#x27;</span>)</span><br><span class="line">        ret = page.evaluate(<span class="string">&#x27;window.navigator.webdriver&#x27;</span>)</span><br><span class="line">        <span class="built_in">print</span>(ret)</span><br><span class="line">        page.wait_for_timeout(<span class="number">30000</span>)</span><br><span class="line">        page.close()</span><br><span class="line">        bro.close()</span><br></pre></td></tr></table></figure>

<h2 id="浏览器接管"><a href="#浏览器接管" class="headerlink" title="浏览器接管"></a>浏览器接管</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">通常情况下，我们使用playwright</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>操作步骤</strong></p>
</blockquote>
<blockquote>
<p>任意目录下新建一个，空白文件夹，用于保存接管的浏览器的运行数据。</p>
</blockquote>
<blockquote>
<p>找到浏览器安装的完整路径。（可执行文件）</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/Applications/Google Chrome.app/Contents/MacOS/Google Chrome</span><br></pre></td></tr></table></figure>

<blockquote>
<p>打开终端，进入浏览器可执行程序目录，输入命令启动浏览器。(可执行程序有空格，需要\进行转义)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd /Applications/Google Chrome.app/Contents/MacOS</span><br><span class="line">./Google\ Chrome --remote-debugging-port=8891 --incognito --start-maximized --user-data-dir=&quot;/Users/houyudong/Desktop/data&quot;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>编写代码实现托管</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">localBro</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        browser = p.chromium.connect_over_cdp(<span class="string">&#x27;http://localhost:8891/&#x27;</span>)</span><br><span class="line">        <span class="comment"># 获取page对象</span></span><br><span class="line">        page = browser.contexts[<span class="number">0</span>].pages[<span class="number">0</span>]</span><br><span class="line">        <span class="comment"># 该操作会直接作用在接管的浏览器中</span></span><br><span class="line">        page.locator(<span class="string">&#x27;//*[@id=&quot;kw&quot;]&#x27;</span>).fill(<span class="string">&#x27;liulanqi&#x27;</span>)</span><br><span class="line">        page.locator(<span class="string">&quot;#su&quot;</span>).click()</span><br><span class="line">        <span class="built_in">print</span>(page.url)</span><br><span class="line">        <span class="built_in">print</span>(page.title())</span><br></pre></td></tr></table></figure>

<h2 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h2><h3 id="搜索关键字"><a href="#搜索关键字" class="headerlink" title="搜索关键字"></a>搜索关键字</h3><blockquote>
<p>示例一 360搜索关键字 第二十条</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 360搜索关键字 第二十条</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">search1</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        webbrowser = p.chromium.launch(headless=<span class="literal">False</span>)</span><br><span class="line">        page = webbrowser.new_page()</span><br><span class="line">        page.goto(<span class="string">&quot;https://www.so.com/&quot;</span>)</span><br><span class="line">        page.wait_for_timeout(<span class="number">2000</span>)</span><br><span class="line">        locator = page.locator(<span class="string">&quot;#input&quot;</span>)</span><br><span class="line">        locator.fill(<span class="string">&quot;第二十条&quot;</span>)</span><br><span class="line">        search_btn = page.locator(<span class="string">&quot;#search-button&quot;</span>)</span><br><span class="line">        search_btn.click()</span><br><span class="line">        page.wait_for_timeout(<span class="number">4000</span>)</span><br><span class="line">        <span class="comment"># ---------------------</span></span><br><span class="line">        page.close()</span><br><span class="line">        webbrowser.close()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>示例二 360搜索关键字 第二十条 模拟认为操作</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 360搜索关键字 第二十条 模拟认为操作</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">search2</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        webbrowser = p.chromium.launch(headless=<span class="literal">False</span>)</span><br><span class="line">        page = webbrowser.new_page()</span><br><span class="line">        page.goto(<span class="string">&quot;https://www.so.com/&quot;</span>)</span><br><span class="line">        page.wait_for_timeout(<span class="number">2000</span>)</span><br><span class="line">        locator = page.locator(<span class="string">&quot;#input&quot;</span>)</span><br><span class="line">        page.keyboard.<span class="built_in">type</span>(<span class="string">&quot;第二十条&quot;</span>,delay=<span class="number">1000</span>)</span><br><span class="line">        search_btn = page.locator(<span class="string">&quot;#search-button&quot;</span>)</span><br><span class="line">        search_btn.click()</span><br><span class="line">        page.wait_for_timeout(<span class="number">4000</span>)</span><br><span class="line">        <span class="comment"># ---------------------</span></span><br><span class="line">        page.close()</span><br><span class="line">        webbrowser.close()</span><br></pre></td></tr></table></figure>

<blockquote>
<p>示例三 page页面常用函数</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#page页面常用函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">page</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        webbrowser = p.chromium.launch(headless=<span class="literal">False</span>)</span><br><span class="line">        page = webbrowser.new_page()</span><br><span class="line">        page.goto(<span class="string">&quot;https://www.so.com/&quot;</span>)</span><br><span class="line">        page.wait_for_timeout(<span class="number">2000</span>)</span><br><span class="line">        locator = page.locator(<span class="string">&quot;#input&quot;</span>)</span><br><span class="line">        page.keyboard.<span class="built_in">type</span>(<span class="string">&quot;第二十条&quot;</span>,delay=<span class="number">1000</span>)</span><br><span class="line">        search_btn = page.locator(<span class="string">&quot;#search-button&quot;</span>)</span><br><span class="line">        search_btn.click()</span><br><span class="line">        page.wait_for_timeout(<span class="number">1000</span>)</span><br><span class="line">        page.go_back()</span><br><span class="line">        page.wait_for_timeout(<span class="number">1000</span>)</span><br><span class="line">        page.go_forward()</span><br><span class="line">        page.wait_for_timeout(<span class="number">1000</span>)</span><br><span class="line"></span><br><span class="line">        title = page.title()</span><br><span class="line">        content =  page.content()</span><br><span class="line">        <span class="built_in">print</span>(title,content)</span><br><span class="line">        <span class="comment"># ---------------------</span></span><br><span class="line">        page.close()</span><br><span class="line">        webbrowser.close()</span><br></pre></td></tr></table></figure>

<blockquote>
<p>示例四 模拟京东登录操作，滑动解锁</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> cv2</span><br><span class="line"><span class="keyword">import</span> base64</span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"><span class="keyword">from</span> io <span class="keyword">import</span> BytesIO</span><br><span class="line"><span class="comment">#Data URL scheme 数据存储为图片</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">savePic</span>(<span class="params">base64_data, output_path</span>):</span><br><span class="line">    <span class="comment"># 解析URL字符串</span></span><br><span class="line">    mediatype, data = base64_data.split(<span class="string">&#x27;,&#x27;</span>)</span><br><span class="line">    encoding = mediatype.split(<span class="string">&#x27;;&#x27;</span>)[<span class="number">1</span>] <span class="keyword">if</span> <span class="string">&#x27;;&#x27;</span> <span class="keyword">in</span> mediatype <span class="keyword">else</span> <span class="string">&#x27;&#x27;</span></span><br><span class="line">    <span class="comment"># 解码数据</span></span><br><span class="line">    <span class="keyword">if</span> encoding == <span class="string">&#x27;base64&#x27;</span>:</span><br><span class="line">        data = base64.b64decode(data)</span><br><span class="line">        <span class="comment"># 保存图像到指定路径</span></span><br><span class="line">        image = Image.<span class="built_in">open</span>(BytesIO(data))</span><br><span class="line">        <span class="comment"># 保存图像到指定路径</span></span><br><span class="line">        image.save(output_path)</span><br><span class="line"><span class="comment">#获取要滑动的距离</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_distance</span>(<span class="params">background,gap</span>):</span><br><span class="line">    <span class="comment">#滑动验证码的整体背景图片</span></span><br><span class="line">    background = cv2.imread(background, <span class="number">0</span>)</span><br><span class="line">    <span class="comment">#缺口图片</span></span><br><span class="line">    gap = cv2.imread(gap, <span class="number">0</span>)</span><br><span class="line">    res = cv2.matchTemplate(background, gap, cv2.TM_CCOEFF_NORMED)</span><br><span class="line">    <span class="comment">#value就是cv2计算出来的滑动距离的像素值</span></span><br><span class="line">    value = cv2.minMaxLoc(res)[<span class="number">2</span>][<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line">    <span class="comment">#单位换算</span></span><br><span class="line">    <span class="keyword">return</span> value * <span class="number">278</span> / <span class="number">360</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#有的检测移动速度的 如果匀速移动会被识别出来，来个简单点的渐进</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_track</span>(<span class="params">distance</span>):  <span class="comment"># distance为传入的总距离</span></span><br><span class="line">    <span class="comment"># 移动轨迹</span></span><br><span class="line">    track = []</span><br><span class="line">    <span class="comment"># 当前位移</span></span><br><span class="line">    current = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 减速阈值</span></span><br><span class="line">    mid = distance * <span class="number">4</span> / <span class="number">5</span></span><br><span class="line">    <span class="comment"># 计算间隔</span></span><br><span class="line">    t = <span class="number">0.2</span></span><br><span class="line">    <span class="comment"># 初速度</span></span><br><span class="line">    v = <span class="number">1</span></span><br><span class="line">    <span class="keyword">while</span> current &lt; distance:</span><br><span class="line">        <span class="keyword">if</span> current &lt; mid:</span><br><span class="line">            <span class="comment"># 加速度为2</span></span><br><span class="line">            a = <span class="number">4</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="comment"># 加速度为-2</span></span><br><span class="line">            a = -<span class="number">3</span></span><br><span class="line">        v0 = v</span><br><span class="line">        <span class="comment"># 当前速度</span></span><br><span class="line">        v = v0 + a * t</span><br><span class="line">        <span class="comment"># 移动距离</span></span><br><span class="line">        move = v0 * t + <span class="number">1</span> / <span class="number">2</span> * a * t * t</span><br><span class="line">        <span class="comment"># 当前位移</span></span><br><span class="line">        current += move</span><br><span class="line">        <span class="comment"># 加入轨迹</span></span><br><span class="line">        track.append(<span class="built_in">round</span>(move))</span><br><span class="line">    <span class="keyword">return</span> track</span><br><span class="line"><span class="keyword">def</span> <span class="title function_">jd_login</span>():</span><br><span class="line">    <span class="keyword">with</span> sync_playwright() <span class="keyword">as</span> p:</span><br><span class="line">        browser = p.chromium.launch(headless=<span class="literal">False</span>)</span><br><span class="line">        page = browser.new_page()</span><br><span class="line">        page.goto(<span class="string">&quot;http://www.jd.com&quot;</span>)</span><br><span class="line">        page.wait_for_timeout(<span class="number">1000</span>)</span><br><span class="line">        page.locator(<span class="string">&quot;#ttbar-login&quot;</span>).click()</span><br><span class="line">        page.wait_for_timeout(<span class="number">1000</span>)</span><br><span class="line">        page.locator(<span class="string">&quot;#loginname&quot;</span>).fill(<span class="string">&quot;east&quot;</span>)</span><br><span class="line">        page.locator(<span class="string">&quot;#nloginpwd&quot;</span>).fill(<span class="string">&quot;1111&quot;</span>)</span><br><span class="line">        page.wait_for_timeout(<span class="number">1000</span>)</span><br><span class="line">        page.locator(<span class="string">&quot;#loginsubmit&quot;</span>).click()</span><br><span class="line">        page.wait_for_timeout(<span class="number">1000</span>)</span><br><span class="line">        img1=page.locator(<span class="string">&quot;//div[@class=&#x27;JDJRV-bigimg&#x27;]/img&quot;</span>)</span><br><span class="line">        img2=page.locator(<span class="string">&quot;//div[@class=&#x27;JDJRV-smallimg&#x27;]/img&quot;</span>)</span><br><span class="line">        savePic(img1.get_attribute(<span class="string">&quot;src&quot;</span>) ,<span class="string">&quot;./big.png&quot;</span>)</span><br><span class="line">        savePic(img2.get_attribute(<span class="string">&quot;src&quot;</span>) ,<span class="string">&quot;./small.png&quot;</span>)</span><br><span class="line">        distance = get_distance(<span class="string">&quot;./big.png&quot;</span>,<span class="string">&quot;./small.png&quot;</span>)</span><br><span class="line">        new_distance = get_track(distance)</span><br><span class="line">        box = page.locator(<span class="string">&quot;.JDJRV-slide-btn&quot;</span>).bounding_box()</span><br><span class="line">        page.mouse.move(box[<span class="string">&quot;x&quot;</span>] + box[<span class="string">&quot;width&quot;</span>] / <span class="number">2</span>, box[<span class="string">&quot;y&quot;</span>] + box[<span class="string">&quot;height&quot;</span>] / <span class="number">2</span>)</span><br><span class="line">        page.mouse.down()</span><br><span class="line">        x = box[<span class="string">&quot;x&quot;</span>] + <span class="number">12</span>  <span class="comment"># 加上一个数值调整滑动误差</span></span><br><span class="line">        <span class="keyword">for</span> track <span class="keyword">in</span> new_distance:</span><br><span class="line">            <span class="comment"># 循环鼠标按照轨迹移动</span></span><br><span class="line">            page.mouse.move(x + track, <span class="number">0</span>)</span><br><span class="line">            x += track</span><br><span class="line">        page.mouse.up()</span><br><span class="line">        <span class="built_in">input</span>()</span><br><span class="line">        page.close()</span><br><span class="line">        browser.close()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://houyudong1989.gitee.io/blog">侯玉东</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://houyudong1989.gitee.io/blog/2024/04/26/python/13%E3%80%81playwright/">https://houyudong1989.gitee.io/blog/2024/04/26/python/13%E3%80%81playwright/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://houyudong1989.gitee.io/blog" target="_blank">East'blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/playwright/">playwright</a></div><div class="post_share"><div class="social-share" data-image="/blog/img/library1.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包"><img class="cover" src="/blog/img/library1.jpeg" onerror="onerror=null;src='/blog/img/friend_404.gif'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">charles 抓包</div></div></a></div><div class="next-post pull-right"><a href="/blog/2024/04/17/python/9%E3%80%81%E7%BA%BF%E7%A8%8B%E3%80%81%E8%BF%9B%E7%A8%8B%E5%92%8C%E5%8D%8F%E7%A8%8B%E6%A6%82%E5%BF%B5/" title="线程、进程和协程概念"><img class="cover" src="/blog/img/library1.jpeg" onerror="onerror=null;src='/blog/img/friend_404.gif'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">线程、进程和协程概念</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/head.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">侯玉东</div><div class="author-info__description">生命不息，奋斗不止</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">70</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/houyudong1989"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/houyudong1989" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:420098321@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到我的博客！！！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#playwright"><span class="toc-number">1.</span> <span class="toc-text">playwright</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85"><span class="toc-number">1.1.</span> <span class="toc-text">环境安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B1%8F%E5%B9%95%E5%BD%95%E5%88%B6"><span class="toc-number">1.2.</span> <span class="toc-text">屏幕录制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BD%95%E5%88%B6%E6%B5%81%E7%A8%8B"><span class="toc-number">1.2.1.</span> <span class="toc-text">录制流程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BD%95%E5%88%B6%E5%91%BD%E4%BB%A4"><span class="toc-number">1.2.2.</span> <span class="toc-text">录制命令</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%9D%E7%95%99cookie%E4%BF%A1%E6%81%AF"><span class="toc-number">1.3.</span> <span class="toc-text">保留cookie信息</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E4%B8%AAplaywright%E7%A8%8B%E5%BA%8F"><span class="toc-number">1.4.</span> <span class="toc-text">第一个playwright程序</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%9A%E4%BD%8D%E5%85%83%E7%B4%A0"><span class="toc-number">1.5.</span> <span class="toc-text">定位元素</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E5%AE%9A%E4%BD%8D"><span class="toc-number">1.5.1.</span> <span class="toc-text">CSS定位</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#css%E5%AE%9A%E4%BD%8D%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.5.1.1.</span> <span class="toc-text">css定位示例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#xpath%E5%AE%9A%E4%BD%8D"><span class="toc-number">1.5.2.</span> <span class="toc-text">xpath定位</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#xpath%E5%AE%9A%E4%BD%8D%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.5.2.1.</span> <span class="toc-text">xpath定位示例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%B9%E6%8D%AE%E6%96%87%E6%9C%AC%E5%AE%9A%E4%BD%8D"><span class="toc-number">1.5.3.</span> <span class="toc-text">根据文本定位</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%87%E6%9C%AC%E5%AE%9A%E4%BD%8D%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.5.3.1.</span> <span class="toc-text">文本定位示例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#iframe%E5%AE%9A%E4%BD%8D"><span class="toc-number">1.5.4.</span> <span class="toc-text">iframe定位</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#frame%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.5.4.1.</span> <span class="toc-text">frame示例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E6%93%8D%E4%BD%9C"><span class="toc-number">1.5.5.</span> <span class="toc-text">其他操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E6%93%8D%E4%BD%9C%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.5.5.1.</span> <span class="toc-text">其他操作示例</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#page-%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0"><span class="toc-number">1.6.</span> <span class="toc-text">page 常用函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#tag%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0"><span class="toc-number">1.7.</span> <span class="toc-text">tag常用函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#context"><span class="toc-number">1.8.</span> <span class="toc-text">context</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#context%E4%BD%9C%E7%94%A8"><span class="toc-number">1.8.1.</span> <span class="toc-text">context作用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#context%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0"><span class="toc-number">1.8.2.</span> <span class="toc-text">context常用函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#context%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.8.3.</span> <span class="toc-text">context示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%A7%84%E9%81%BF%E6%A3%80%E6%B5%8B"><span class="toc-number">1.9.</span> <span class="toc-text">规避检测</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8Btaobao%E6%98%AF%E5%90%A6%E6%A3%80%E6%B5%8BPlaywright"><span class="toc-number">1.9.1.</span> <span class="toc-text">查看taobao是否检测Playwright</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#js%E6%B3%A8%E5%85%A5%EF%BC%8C%E7%BB%95%E8%BF%87%E6%B7%98%E5%AE%9D%E6%A3%80%E6%B5%8B"><span class="toc-number">1.9.2.</span> <span class="toc-text">js注入，绕过淘宝检测</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E6%8E%A5%E7%AE%A1"><span class="toc-number">1.10.</span> <span class="toc-text">浏览器接管</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.11.</span> <span class="toc-text">示例</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%90%9C%E7%B4%A2%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">1.11.1.</span> <span class="toc-text">搜索关键字</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/06/c++/5%E3%80%81%E5%AD%97%E7%AC%A6%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2/" title="字符和字符串"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="字符和字符串"/></a><div class="content"><a class="title" href="/blog/2024/05/06/c++/5%E3%80%81%E5%AD%97%E7%AC%A6%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2/" title="字符和字符串">字符和字符串</a><time datetime="2024-05-06T11:15:27.280Z" title="发表于 2024-05-06 19:15:27">2024-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/06/c++/4%E3%80%81%E6%8C%87%E9%92%88%E5%92%8C%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" title="指针和函数指针"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="指针和函数指针"/></a><div class="content"><a class="title" href="/blog/2024/05/06/c++/4%E3%80%81%E6%8C%87%E9%92%88%E5%92%8C%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" title="指针和函数指针">指针和函数指针</a><time datetime="2024-05-06T11:14:29.047Z" title="发表于 2024-05-06 19:14:29">2024-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/05/python/7%E3%80%81m3u8%E8%A7%86%E9%A2%91%E6%A0%BC%E5%BC%8F/" title="m3u8格式"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="m3u8格式"/></a><div class="content"><a class="title" href="/blog/2024/05/05/python/7%E3%80%81m3u8%E8%A7%86%E9%A2%91%E6%A0%BC%E5%BC%8F/" title="m3u8格式">m3u8格式</a><time datetime="2024-05-05T01:43:26.865Z" title="发表于 2024-05-05 09:43:26">2024-05-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="charles 抓包"/></a><div class="content"><a class="title" href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包">charles 抓包</a><time datetime="2024-04-30T06:49:29.279Z" title="发表于 2024-04-30 14:49:29">2024-04-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/04/26/python/13%E3%80%81playwright/" title="playwright"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="playwright"/></a><div class="content"><a class="title" href="/blog/2024/04/26/python/13%E3%80%81playwright/" title="playwright">playwright</a><time datetime="2024-04-26T07:30:36.069Z" title="发表于 2024-04-26 15:30:36">2024-04-26</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By 侯玉东</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="8873950678" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true"> </div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="false"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener
  btf.removeGlobalFnEvent('pjax')
  btf.removeGlobalFnEvent('themeChange')

  document.getElementById('rightside').classList.remove('rightside-show')
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', e => {
  if (e.request.status === 404) {
    pjax.loadUrl('/blog/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>